package com.zjht.utils;

import java.util.ResourceBundle;

import com.alibaba.fastjson.JSON;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Eirui
 *
 */
public class RedisClient {

    public static JedisPool jedisPool; // 池化管理jedis链接池


    static {
        ResourceBundle bundle = ResourceBundle.getBundle("redis");
        if (bundle == null) { throw new IllegalArgumentException("[redis.properties] is not found!"); }
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
        config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
        config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
        config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
        String redisIp = bundle.getString("redis.ip");
        Integer redisPort = Integer.valueOf(bundle.getString("redis.port"));
        Integer redisTimeOut = Integer.valueOf(bundle.getString("redis.time.out"));
        jedisPool = new JedisPool(config, redisIp, redisPort, redisTimeOut);
    }

    public static JedisPool getJedisPoolInstance() {
        return jedisPool;
    }

    /**
     * 向缓存中设置字符串内容
     * 
     * @param key key
     * @param value value
     * @return
     * @throws Exception
     */
    public static boolean set(String key, String value) throws Exception {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            return false;
        } finally {
            if(jedis != null && jedis.isConnected()){
                jedisPool.returnResource(jedis);
            }
        }
    }

    /**
     * 向缓存中设置对象
     * 
     * @param key
     * @param value
     * @return
     */
    public static boolean set(String key, Object value) {
        Jedis jedis = null;
        try {
            String objectJson = JSON.toJSONString(value);
            jedis = jedisPool.getResource();
            jedis.set(key, objectJson);
            return true;
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            return false;
        } finally {
            if(jedis != null && jedis.isConnected()){
            jedisPool.returnResource(jedis);
            }
        }
    }

    /**
     * 删除缓存中得对象，根据key
     * 
     * @param key
     * @return
     */
    public static boolean del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
            return true;
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            return false;
        } finally {
            if(jedis != null && jedis.isConnected()){
                jedisPool.returnResource(jedis);
            }
        }
    }

    /**
     * 根据key 获取内容
     * 
     * @param key
     * @return
     */
    public static Object get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Object value = jedis.get(key);
            return value;
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            return false;
        } finally {
            if(jedis != null && jedis.isConnected()){
                jedisPool.returnResource(jedis);
            }
        }
    }

    /**
     * 根据key 获取对象
     * 
     * @param key
     * @return
     */
    public static <T> T get(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            return JSON.parseObject(value, clazz);
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            return null;
        } finally {
            if(jedis != null && jedis.isConnected()){
                jedisPool.returnResource(jedis);
            }
        }
    }

    public static Long getRedisDBSize(){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long value = jedis.dbSize();
            return value;
        } catch (Exception e) {
            jedisPool.returnBrokenResource(jedis);
            return 0L;
        } finally {
            if(jedis != null && jedis.isConnected()){
                jedisPool.returnResource(jedis);
            }
        }
    }

}
